<?php

/**
 * ticket actions.
 *
 * @package	FBS
 * @subpackage ticket
 * @author	 Your name here
 * @version	SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 *
 * @property sfDoctrinePager $pageR
 * @property Doctrine_Collection $tickets
 * @property array|Doctrine_Collection|Doctrine_Record $ticket
 * @property TicketForm $form
 */
class ticketActions extends sfActions {

	/**
	 * @param sfWebRequest $request
	 * @return void
	 */
	public function executeIndex(sfWebRequest $request) {

		switch ($request->getParameter('orderBy', 'id')) {
			case 'priority':
				$query = Doctrine::getTable('Ticket')->createQuery()->addOrderBy('priority DESC');
				break;

			default:
				$query = Doctrine::getTable('Ticket')->createQuery()->addOrderBy('id DESC');
		}

		$this->pager = new sfDoctrinePager('Ticket', 10);
		$this->pager->setQuery($query);
		$this->pager->setPage($request->getParameter('page', 1));
		$this->pager->init();
	}

	/**
	 * @param sfWebRequest $request
	 * @return void
	 */
	public function executeShow(sfWebRequest $request) {
		$this->ticket = Doctrine::getTable('Ticket')->find(array( $request->getParameter('id') ));
		$this->forward404Unless((bool)$this->ticket);
	}

	/**
	 * @param sfWebRequest $request
	 * @return void
	 */
	public function executeNew(sfWebRequest $request) {
		$this->form = new TicketForm();
	}

	/**
	 * @param sfWebRequest $request
	 * @return void
	 */
	public function executeCreate(sfWebRequest $request) {
		$this->forward404Unless($request->isMethod(sfRequest::POST));

		$this->form = new TicketForm();

		$this->processForm($request, $this->form);

		$this->setTemplate('new');
	}

	/**
	 * @param sfWebRequest $request
	 * @return void
	 */
	public function executeEdit(sfWebRequest $request) {
		$this->forward404Unless($ticket = Doctrine::getTable('Ticket')->find(array( $request->getParameter('id') )), sprintf('Object ticket does not exist (%s).', $request->getParameter('id')));
		$this->form = new TicketForm($ticket);
	}

	/**
	 * @param sfWebRequest $request
	 * @return void
	 */
	public function executeUpdate(sfWebRequest $request) {
		$this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
		$this->forward404Unless($ticket = Doctrine::getTable('Ticket')->find(array( $request->getParameter('id') )), sprintf('Object ticket does not exist (%s).', $request->getParameter('id')));
		$this->form = new TicketForm($ticket);

		$this->processForm($request, $this->form);

		$this->setTemplate('edit');
	}

	/**
	 * @param sfWebRequest $request
	 * @return void
	 */
	public function executeDelete(sfWebRequest $request) {
		$request->checkCSRFProtection();

		$this->forward404Unless($ticket = Doctrine::getTable('Ticket')->find(array( $request->getParameter('id') )), sprintf('Object ticket does not exist (%s).', $request->getParameter('id')));
		$ticket->delete();

		$this->redirect('ticket/index');
	}

	/**
	 * @param sfWebRequest $request
	 * @param sfForm $form
	 * @return void
	 */
	protected function processForm(sfWebRequest $request, sfForm $form) {
		$form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
		if ($form->isValid()) {
			$ticket = $form->save();

			$this->redirect('ticket/edit?id=' . $ticket->getId());
		}
	}

	/**
	 * @param sfWebRequest $request
	 * @return void
	 */
	public function executeSearch(sfWebRequest $request) {
		$this->forwardUnless($query = $request->getParameter('query'), 'ticket', 'index');

		$this->tickets = Doctrine_Core::getTable('Ticket')->getForLuceneQuery($query);
	}

}
